package com.cloud.scheduledbalance;

import com.cloud.scheduledbalance.annotaion.ScheduledBalance;
import com.cloud.scheduledbalance.annotaion.ScheduledBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.cloud.scheduledbalance
 * @date:2021/1/23
 */
public class ScheduledLifeCycle implements SmartLifecycle {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    ScheduledEurekaServerRegister scheduledEurekaServerRegister;

    @Override
    public void start() {
        Map<String, Object> map = applicationContext.getBeansWithAnnotation(ScheduledBean.class);
        for (String key : map.keySet()) {
            Object o = map.get(key);
            Method[] ms = o.getClass().getDeclaredMethods();
            for (Method m : ms) {
                Object o2 = AnnotationUtils.findAnnotation(m, ScheduledBalance.class);
                if (null != o2) {
                    scheduledEurekaServerRegister.register(m.getName());
                }
            }
        }
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isRunning() {
        return false;
    }
}
